package com.songlanyun.modules.category.entity;

import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.songlanyun.common.drive.RootEntity;
import com.songlanyun.common.utils.SpringContextUtils;
import com.songlanyun.common.validator.group.AddGroup;
import com.songlanyun.common.validator.group.UpdateGroup;
import com.songlanyun.modules.category.service.SystemProductCategoryService;
import lombok.Data;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.*;
import java.io.Serializable;
import java.util.List;

/**
 * 平台的商品分类表
 *
 * @author lubo
 * @email sunlightcs@gmail.com
 * @date 2020-11-02 18:09:51
 */
@Data
@TableName("yxt_system_product_category")
public class SystemProductCategoryEntity extends RootEntity<Long, SystemProductCategoryEntity> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     *
     */
    @TableId
    @NotNull(message = "修改时必须指定商品分类Id", groups = {UpdateGroup.class})
    @Null(message = "新增时不能指定商品分类Id", groups = {AddGroup.class})
    private Long id;
    /**
     * 上级分类
     */
    private Long parentId;
    /**
     * 分类名称
     */
    @NotBlank(message = "商品分类名称不能为空", groups = {AddGroup.class, UpdateGroup.class})
    @Length(min = 1, max = 4, message = "商品分类名称长度在1-4个字", groups = {AddGroup.class, UpdateGroup.class})
    private String name;

    /**
     * 分组图标
     */
    @Length(max = 500, message = "商品分组图标名称长度在500个字", groups = {AddGroup.class, UpdateGroup.class})
    private String coverImage;

    /**
     * 分类说明
     */
    @Length(max = 200, message = "分类说明长度在1-200个字", groups = {AddGroup.class, UpdateGroup.class})
    private String remark;
    /**
     * 分类的顺序
     */
    @Max(value = 999999, message = "顺序值最大999999", groups = {AddGroup.class, UpdateGroup.class})
    @Min(value = 0, message = "最小顺序值是0", groups = {AddGroup.class, UpdateGroup.class})
    private Integer orderIndex;

    /**
     * 是否推荐到
     */
    private Boolean recommend;

    @Override
    public Long getIdentity() {
        return getId();
    }


    /**
     * 获得所有子分类
     *
     * @return
     */
    public List<SystemProductCategoryEntity> loadChildren() {
        return ((SystemProductCategoryService) SpringContextUtils.getBean("systemProductCategoryService")).getChildren(this.getId());
    }

}
